package Servlet;

import dao.MyConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class synServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        int type = Integer.parseInt(request.getParameter("type"));
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        int st = -1;
        PreparedStatement ps;
        try {
            if (type == 0) {
                CallableStatement cs = myCon.connection.prepareCall("call updatedate");
                st = cs.executeUpdate();
                cs.close();
            } else {
                int days = Integer.parseInt(request.getParameter("dis"));
                ps = myCon.connection.prepareStatement("update placetime set PlaceDate=DATE_ADD(PlaceDate,INTERVAL ? day) , PlaceStatus=1");
                ps.setInt(1, days);
                st = ps.executeUpdate();
                ps = myCon.connection.prepareStatement("update placedate set PlaceDate=DATE_ADD(PlaceDate,INTERVAL ? day)");
                ps.setInt(1, days);
                st = ps.executeUpdate();
                ps = myCon.connection.prepareStatement("update appointment set AppointmentCheckStatus=3 where AppointmentTime<CURRENT_DATE()");
                st = ps.executeUpdate();
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        out.print(st);
    }
}
